/**
 * Created by WangMing on 15/12/9.
 */
define(["../../../service",
    "../../../widgets/player_live_widget/player_live_widget"
], function(api) {
    // 定义所有相关的vmodelplayerlivewidgetplayerlivewidget
    var vm = avalon.define({
        $id: "online",
        $player: {
             stateName :"onlines.item"
        },
        video:{
            RecordEquipment:"",
            RecordEquipmentCode:""
        },
        $breadcrumb: {
            stateName: "onlines.item"
        },

        $skipArry: ["playerWidget"],
        _route:[],
        _sub:"",
        _uri:{},
        CTRL:false,
        setSub: function(id){
            vm._sub = id;
            vm._uri = {};
            vm.$fire("down!subChange",vm._sub);
            vm.getOnlineState()
           
        },
        getOnlineState: function() {
            return api.getItems({
                method: api.getOnlineuri,
                query: {
                    id: vm.itemID,
                    sub:vm._sub
                }
            }, function(res) {

                if(vm._sub != res.data.Sub&&vm._sub != ""&&vm._sub!=-1)
                    return;
                if (!vm.$player.VideoResourcePath) vm.$player.VideoResourcePath = {};
                
                vm.$player.VideoResourcePath = _.merge(vm.$player.VideoResourcePath, res);

                if(res.data){
                    vm.video.Video.liveTitle = res.data.title;
                    vm.video.Video.title = res.data.title;
                    vm.video.Video.liveSpeaker = res.data.speaker
                    vm.video.RecordEquipment = res.data.RecordEquipment
                    vm.video.RecordEquipmentCode = res.data.RecordEquipmentCode
                }
                var uri = (res.data.Uri&&$.parseJSON(res.data.Uri))||{}
                for (var idx in uri) {
                    if (idx.indexOf("ERROR") != -1) {
                        delete uri[idx]
                    }
                }
                if(res.data&&res.data.Route){
                    var _route = $.parseJSON(res.data.Route)
                    if(_route){
                        vm._route = [];
                        vm._route = _route
                    }
                }else{
                    vm._route = []
                }

                
                (vm._sub==""||vm._sub==-1)&&(vm._sub = res.data.Sub);


                if(res.data.Uri){
                    vm._uri = $.parseJSON(res.data.Uri);
                }else {
                    vm._uri = {}
                }
                vm.$player.VideoResourcePath.data.Uri = uri;

                if(vm._route&&vm._route.length  != 0){
                    if(vm._sub == -1){
                        vm.$player.error = {
                            errorCode : 5,
                            message:"请选择播放线路"
                        }
                    }else{
                        if(_.isEmpty(uri)){
                            if(vm._route.length){
                                vm.$player.error = {
                                    errorCode : 5,
                                    message:"请尝试其他播放线路"
                                }
                            }
                            
                        }else{
                            vm.$player.error = {
                                errorCode : 0,
                                message:""
                            }
                        }
                    }
                }else{
                    vm.$player.error = {
                        errorCode : 0,
                        message:""
                    }
                }
                vm.$fire("down!onlineStateChange", vm.$player);
                
            })

        }
    });
    vm.$watch("onlineRefresh",vm.getOnlineState)

    return avalon.controller(function($ctrl) {
        // 视图渲染后，意思是avalon.scan完成
        $ctrl.$onRendered = function() {

           api.getItems({
                method:api.setLivePv,
                query:{
                    id:vm.itemID
                }
            },function(res){
                
            })
           document.onkeydown  =  function(evt){
                evt = (evt) ? evt : window.event
                if (evt.keyCode) {
                   if(evt.keyCode == 17){
                     vm.CTRL = true
                   }
                }
            }
            document.onkeyup = function(){
                vm.CTRL = false
            }
                
        };
        // 进入视图
        $ctrl.$onEnter = function(param, rs, rj) {
            vm.video.$player = vm.$player;
            vm.itemID = param.itemID
            return new Promise(function(resolve, reject) {
                resolve()
            }).then(function() {

                return api.getItems({
                    method: api.getOnline,
                    query: {
                        id: param.itemID 
                    }
                }, function(res) {
                    if (!vm.video.Video) vm.video.Video = {};
                    vm.video.Video = avalon.define(avalon.mix(vm.video.Video, res.data))
                })
            }).then(function(){
                vm.getOnlineState()
            })

        };
        // 对应的视图销毁前
        $ctrl.$onBeforeUnload = function() {
            vm.getOnlineStateRefresh = false;
            vm._route = [];
            vm._sub = "";
            vm._uri = {}

        };
        $ctrl.$vmodels = [vm];
        avalon.scan();
    })
});
